package de.ifi.lmu.snitzelhunt.database;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

/**
 * Helper Class for opening the local Database of this Application
 * 
 * @author jaschkowitz
 * 
 */
public class DBHelper extends SQLiteOpenHelper {

	// If you change the database schema, you must increment the database
	// version.
	public static final int DATABASE_VERSION = 3;
	public static final String DATABASE_NAME = "Snitzelhunt_db3";

	// DHelper as Singleton because of Multithreading
	private static DBHelper instance;

	/**
	 * gets the DBHelper with is implemented as Singleton because of concurrency
	 * issues
	 * 
	 * @param context
	 * @return DBHelper
	 */
	public static synchronized DBHelper getHelper(Context context) {
		if (instance == null)
			instance = new DBHelper(context);

		return instance;
	}

	public DBHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	public void onCreate(SQLiteDatabase db) {
		db.execSQL(RouteTable.SQL_CREATE_TABLE);
	}

	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL(RouteTable.SQL_DELETE_TABLE);
		onCreate(db);
	}

	public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		onUpgrade(db, oldVersion, newVersion);
	}
}
